package com.qidian.mes.desensitivity;/**
 * ClassName:DesensitivityUtilQc
 * Package:com.qidian.mes.desensitivity
 **/

import java.lang.reflect.Field;
import java.util.Arrays;

/**
 * @Author ppc
 * @Conpany study
 * @Description:
 * @Time 2024/4/12 17:05
 *
 **/
public class DesensitivityUtilQc {

    public static <T> T desensitization(T t){
        //通过反射获取所有的字段
        Class<?> clazz = t.getClass();
        Field[] declaredFields = clazz.getDeclaredFields();
        //遍历字段并进行判断
        Arrays.stream(declaredFields).forEach(field -> {
            field.setAccessible(true);
            //判断字段是否被注解
            if(field.isAnnotationPresent(DesensitivityAnnotationQc.class) && field.getType() == String.class){
                //获取注解
                DesensitivityAnnotationQc annotation = field.getAnnotation(DesensitivityAnnotationQc.class);
                //获取注解的值
                String value = annotation.value();
                int length = annotation.length();
                int prefixLength = annotation.prefixNoMaskLength();
                int suffixLength = annotation.suffixNoMaskLength();
                //获取属性的值
                try {
                    String fieldVale = field.get(t).toString();

                    //脱敏处理
                    String pre = fieldVale.substring(0,prefixLength);
                    String suff = fieldVale.substring(fieldVale.length()-suffixLength);
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < length; i++) {
                        sb.append(value);

                    }
                    field.set(t,pre+sb.toString()+suff);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(e);
                }

            }
        });
        return t;
    }



}
